<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of NetworkController
 *
 * @author Dennis
 */
class Admin_KlantenController extends Zend_Controller_Action {

    protected $_klanten;

    public function init() {
        $this->view->url = $this->_helper->mvcUrl();
        $this->_klanten = new Default_Model_DbTable_Klanten();
    }

    public function indexAction() {
        $this->view->entries = $this->_klanten->fetchAll($this->_klanten->select()->from($this->_klanten, array('id', 'klant')))->toArray();
    }

    public function addAction() {
        $form = $this->_helper->form('klant');
        $this->view->form = $form;
        if($this->getRequest()->isPost()) {
            if($form->isValid($this->getRequest()->getPost())||!$form->plaatje->receive()) {

                $values = $form->getValues();
                $data = array(
                        'id' => null,
                        'klant' => $values['klant'],
                        'datum' => $values['datum'],
                        'url' => $values['url'],
                        'owner' => Zend_Auth::getInstance()->getStorage()->read()->id
                );

                if($form->plaatje->isUploaded()) {
                    $fileName = $form->plaatje->getFileName();
                    $tcImage = new Tc_Image();
                    $tcImage->crop($fileName, '174', '164');

                    $saveFileName =  date('YmdHis') . ".png";
                    $tcImage->saveImagePng(Zend_Registry::get('uploadPath') . $saveFileName);
                    
                    $data['plaatje'] = $saveFileName;
                }

                $this->_klanten->insert($data);
                return $this->_helper->redirector('index');
            }
        }
    }

    public function editAction() {
        $form = $this->_helper->form('klant');
        $form->setAction($this->view->url['edit']."/id/".$this->getRequest()->getParam('id'));

        $message = $this->_klanten->find($this->getRequest()->getParam('id'));
        $message = $message[0];

        if($this->getRequest()->isPost()) {
            if($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $data = array(
                       'klant' => $values['klant'],
                        'datum' => $values['datum'],
                        'url' => $values['url'],
                );
                
                if($form->plaatje->isUploaded()) {

                    /*Oude plaaje eerst verwijderen*/

                    unlink(Zend_Registry::get('uploadPath') . $message['plaatje']);

                    $fileName = $form->plaatje->getFileName();
                    $tcImage = new Tc_Image();
                    $tcImage->crop($fileName, '174', '164');

                    $saveFileName =  date('YmdHis') . ".png";
                    $tcImage->saveImagePng(Zend_Registry::get('uploadPath') . $saveFileName);

                    $data['plaatje'] = $saveFileName;
                }

                $this->_klanten->update($data, $this->_klanten->getAdapter()->quoteInto('id = ?', $this->getRequest()->getParam('id')));
                return $this->_helper->redirector('index');
            }

        }else {
            $element = $form->getelement('klant');
            $element->setValue($message['klant']);
            $element = $form->getelement('datum');
            $element->setValue($message['datum']);
            $element = $form->getelement('url');
            $element->setValue($message['url']);
        }
        $this->view->form = $form;
    }

    public function deleteAction() {

        /*Eerst oude plaatje verwijderen*/
        $message = $this->_klanten->find($this->getRequest()->getParam('id'));
        $message = $message[0];

        unlink(Zend_Registry::get('uploadPath') . $message['plaatje']);

        $this->_klanten->delete($this->_klanten->getAdapter()->quoteInto('id = ?', $this->getRequest()->getParam('id')));
        return $this->_helper->redirector('index');
    }

}
?>
